import datetime

from PySide2.QtCore import QThread, QObject, Signal, Slot, QWaitCondition, QMutex, QDeadlineTimer
from PySide2.QtWidgets import QApplication, QDialog, QLineEdit, QPushButton, QVBoxLayout, QLabel
import threading
from src.LogPlayer.utils.Worker import Worker, Message

class MyInputDialog(QDialog):
    label_singal = Signal(str)
    table_singal = Signal(str)
    def __init__(self):
        super().__init__()

        #
        # 输入框
        #
        self.input = QLineEdit()

        #
        # label 标签
        #
        self.label_1 = QLabel()

        #
        # label_2
        #
        self.label_2 = QLabel()

        #
        # 发送按钮
        #
        self.ok_btn = QPushButton(text="发送")
        self.ok_btn.clicked.connect(self.submit)

        layout = QVBoxLayout()
        layout.addWidget(self.input)
        layout.addWidget(self.label_1)
        layout.addWidget(self.label_2)
        layout.addWidget(self.ok_btn)
        self.setLayout(layout)

        self.worker = Worker()
        self.worker.start()
        self.label_singal.connect(self.update_label1)
        self.table_singal.connect(self.update_label2)

    @Slot(str)
    def update_label1(self, result):
        print('in parent @Slot function, thread = ' + str(threading.current_thread()))
        self.label_1.setText("更新标签成功：" + result)

    @Slot(str)
    def update_label2(self, result):
        self.label_2.setText("更新标签成功：" + result)

    def submit(self):
        print('in ui clicked connected function, thread = ' + str(threading.current_thread()))
        msg = Message()
        msg.onBackground = self.getInput
        msg.onFinish = self.label_singal
        self.worker.postDelay(msg, 2000)

    def getInput(self) -> str:
        return self.input.text()

def get_time() -> str:
    """
    获取当前时间，返回时间字符串，如：2020-06-12 11:13:22
    :return:时间字符串
    """
    now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    return now_time

if __name__ == '__main__':
    app = QApplication()

    #
    # 显示输入框
    #
    dial = MyInputDialog()
    dial.show()

    app.exec_()
